package com.yu.util;

import com.yu.domain.Product;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@Component
public class HtmlParseUtil {
    public static void main(String[] args) throws IOException {
//        //获得请求
//        String url = "https://search.jd.com/Search?keyword=java";
//        //解析网页,Jsoup返回的document就是浏览器的document对象
//        Document document = Jsoup.parse(new URL(url), 30000);
//        //所有在js中使用的操作doc的方法都可以在这里使用
//        Element element = document.getElementById("J_goodsList");
////        System.out.println(element.html());
//        Elements lis = element.getElementsByTag("li");
//        //
//        for (Element li : lis) {
//            String img = li.getElementsByTag("img").eq(0).attr("src");
//            String price = li.getElementsByClass("p-price").eq(0).text();
//            String title = li.getElementsByClass("p-name").eq(0).text();
//            System.out.print("图片："+img+"   ");
//            System.out.print("价格："+price+"   ");
//            System.out.print("标题："+title+"   ");
//            System.out.println();
//            System.out.println("====================================================");
//
//        }
        HtmlParseUtil util = new HtmlParseUtil();
        List<Product> list = util.parseJD("奶粉");
        System.out.println(list.size());
    }
    public List<Product> parseJD(String keyword) throws IOException {
            List<Product> list = new ArrayList<>();
            //获得请求
            String url = "https://search.jd.com/Search?keyword="+keyword;
            //解决因为中文字符出现导致url出现乱码的问题
            String encode = URLEncoder.encode(url, "utf-8");
            url = URLDecoder.decode(encode,"utf-8");
            System.out.println(url);
        //解析网页,Jsoup返回的document就是浏览器的document对象
            Document document = Jsoup.parse(new URL(url), 30000);
            //所有在js中使用的操作doc的方法都可以在这里使用
            Element element = document.getElementById("J_goodsList");
            //System.out.println(element.html());
            Elements lis = element.getElementsByTag("li");
            //
            for (Element li : lis) {
                String img = li.getElementsByTag("img").eq(0).attr("src");
                String price = li.getElementsByClass("p-price").eq(0).text();
                String title = li.getElementsByClass("p-name").eq(0).text();
                String shop = li.getElementsByClass("p-shop").eq(0).text();
                if (shop.isEmpty()){  //bug 有些商品的店铺标签不为p-shop
                    shop = li.getElementsByClass("p-shopnum").eq(0).text();

                }
                System.out.print("图片："+img+"   ");
                System.out.print("价格："+price+"   ");
                System.out.print("标题："+title+"   ");
                System.out.print("店家："+title+"   ");
                System.out.println();
                System.out.println("====================================================");
                Product product = new Product();
                product.setImg(img);
                product.setTitle(title);
                product.setPrice(price);
                product.setShop(shop);
                list.add(product);
            }
            return list;
    }
}
